package com.smag.web.task;

import com.smag.common.constant.SystemConstants;
import com.smag.common.core.domain.entity.FarmArticle;
import com.smag.common.core.redis.RedisCache;
import com.smag.system.service.FarmArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.smag.common.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private FarmArticleService farmArticleService;

    @Scheduled(cron = "*/11 * * * * ?")
    public void updateViewCountJob() {
//        System.out.println("文章浏览量更新");
        //获取redis中的浏览量
        Map<String,Integer> viewCountMap = redisCache.getCacheMap(SystemConstants.VIEW_COUNT);
        List<FarmArticle> farmArticles = viewCountMap.entrySet()
                .stream()
                .map(entry -> new FarmArticle(Long.valueOf(entry.getKey()), entry.getValue().longValue()))
                .collect(Collectors.toList());
        //更新到数据库中
        farmArticleService.updateBatchById(farmArticles);
    }

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
}
